//
//  BottomBarViewController.m
//  oschina_song
//
//  Created by song on 16/2/1.
//  Copyright © 2016年 song. All rights reserved.
//

#import "BottomBarViewController.h"
#import "EmojiPageVC.h"
#import "EmojiPanelVC.h"
#import "UIColor+utils.h"

@interface BottomBarViewController ()

@property(nonatomic , strong) EmojiPageVC *emojiPageVC;
@property(nonatomic, assign) BOOL isEmojiPageOnScreen;

@end



@implementation BottomBarViewController



-(id)initWithModeSwitchButton:(BOOL)hasAModeSwithButton
{
    self = [super init];
    if(self)
    {
        _editingBar =[[EditingBar alloc] initWithModeSwitchButton:hasAModeSwithButton];
       if(hasAModeSwithButton)
       {
           _hasAModeSwithButton = hasAModeSwithButton;
           _operationBar = [OperationBar new];
           _operationBar.hidden = YES;
       }

        
    }
    
    return self;
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor themeColor];
    [self setup];
    
}


-(void)setup
{
    [self addBottomBar];
    
    _emojiPageVC  = [[EmojiPageVC alloc] initWithTextView:nil];
    _emojiPageVC.view.hidden = YES;
    [self.view addSubview:_emojiPageVC.view];
    _emojiPageVC.view.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = @{@"emojiPage":_emojiPageVC.view};
    [self.view  addSubview:_emojiPageVC.view];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[emojiPage(216)]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[emojiPage]|" options:0 metrics:nil views:views]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidUpdate:)    name:UITextViewTextDidChangeNotification object:nil];
    
}

-(void)addBottomBar
{
    
    _editingBar.translatesAutoresizingMaskIntoConstraints = NO;
    
    [_editingBar.inputViewButton addTarget:self action:@selector(switchInputView) forControlEvents:UIControlEventTouchUpInside];
    [_editingBar.modeSwitchButton addTarget:self action:@selector(swichMode) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_editingBar];
    _editingBarYConstraint =[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_editingBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
    _editingBarHeightConstraint =[NSLayoutConstraint constraintWithItem:_editingBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[self minimumInputbarHeight]];
    
    [self.view addConstraint:_editingBarYConstraint];
    [self.view addConstraint:_editingBarHeightConstraint];
    
    
    _operationBar.backgroundColor = [UIColor grayColor];
    [self.view bringSubviewToFront:(UIView *)_operationBar];
    [_operationBar.modeSwitchButton addTarget:self action:@selector(swichMode) forControlEvents:UIControlEventTouchUpInside];
    _operationBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_operationBar];
    
    __weak BottomBarViewController *weakSelf = self;
    
    _operationBar.swichMode = ^{[weakSelf swichMode];};
    
    
    NSDictionary *metrics = @{@"height":@([self minimumInputbarHeight])};
    NSDictionary *views = NSDictionaryOfVariableBindings(_operationBar);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_operationBar]|" options:0 metrics:metrics views:views]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_operationBar]|" options:0 metrics:nil views:views]];
}


-(void)switchInputView
{
    if(_isEmojiPageOnScreen)
    {
        _emojiPageVC.view.hidden = YES;
        [_editingBar.editView becomeFirstResponder];
        [_editingBar.inputViewButton setImage:[UIImage imageNamed:@"toolbar-emoji2"] forState:UIControlStateNormal];
        _isEmojiPageOnScreen = NO;
    }else{
        [_editingBar.editView resignFirstResponder];
        [_editingBar.inputViewButton setImage:[UIImage imageNamed:@"toolbar-text"] forState:UIControlStateNormal];
        _editingBarYConstraint.constant = 500;
        [self setBottomBarHeight];
        _emojiPageVC.view.hidden = NO;
        _isEmojiPageOnScreen = YES;
    }
}

-(void)swichMode
{
    if(_operationBar.isHidden){
        [_editingBar.editView resignFirstResponder];
        _editingBar.hidden =YES;
        _operationBar.hidden =NO;
    }else {
        _editingBar.hidden =NO;
        _operationBar.hidden =YES;
    }
}

#pragma mark - 调整bar的高度

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGRect keyboardBounds = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _editingBarYConstraint.constant = keyboardBounds.size.height;
    
    _emojiPageVC.view.hidden = YES;
    _isEmojiPageOnScreen = NO;
    [_editingBar.inputViewButton setImage:[UIImage imageNamed:@"toolbar-emoji2"] forState:UIControlStateNormal];
    
    [self setBottomBarHeight];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    _editingBarYConstraint.constant = 0;
    
    [self setBottomBarHeight];
}

-(void)setBottomBarHeight
{
#if 0
    NSTimeInterval animationDuration;
    [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    
    UIViewKeyframeAnimationOptions animationOptions;
    animationOptions = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
#endif
    [self.view setNeedsUpdateConstraints];
    [UIView animateKeyframesWithDuration:0.25       //animationDuration
                                   delay:0
                                 options:7 << 16    //animationOptions
                              animations:^{
                                  [self.view layoutIfNeeded];
                              } completion:nil];
    
}

-(CGFloat)minimumInputbarHeight
{
   return  _editingBar.intrinsicContentSize.height;
}

@end
